<script src="/js/jquery-ui-timepicker-addon.js"></script>
<script>
$(document).ready(function() { 
    $("#myTable").tablesorter({sortList:[[0,0]]}); 
        
     //$(".dia").click(function (e) { 
     
     $("table.calendar").on("click", "div.dia", function(e){
         e.preventDefault();
            
         $.get('/intervenciones/insertar/fecha/'+this.id, function(data)
         {
			$('#dialog')
            .html(data)
            .dialog({buttons: {	"Guardar": function() {$("form#ajax_form").submit() }}})
            .dialog('open');
            $('.fecha').datepicker();   
		 });
            
     });


     $("table.calendar").on("click", "a.editar_inter", function(e){
    	e.preventDefault();
        
    	
        var idli= $(this).closest('li').attr('id')
       
        var aux=idli.split('-');
        id=aux[1]
        
   
        $.get('/intervenciones/editar/id/'+id, function(data)
        {
			$('#dialog')
            .html(data)
            .dialog({buttons: {	"Guardar": function() {$("form#ajax_form").submit() }}})
            .dialog('open');
            $('.fecha').datepicker();   
		});
        
    })
    .css('cursor','pointer');
    
    
    $("table.calendar").on("click", "a.borrar_inter", function(event){
        event.preventDefault();
        var li= $(this).closest('li')
        var idli= li.attr('id')
        var aux=idli.split('-');
        var id=aux[1]
       

        $('#dialog')
        .html('<p><span class="ui-icon ui-icon-alert"></span>Este item sera borrado permanentemente y no se podra recuperar. ¿Estas seguro?</p>')
        .dialog( "option", "title", "¿Seguro que desea Borrar?" )
        .dialog({buttons: { 
        			"Borrar": function() { 
        				$.get('/intervenciones/borrar/id/'+id, function(data){
                            if(data.status=='ok'){
                                li.fadeOut();
                                $('#dialog').dialog("close");
                            }else{
                                alert(data);
                                $('#dialog').dialog("close");
                            }
                        })
        			} ,
        			"Cancelar": function() { $(this).dialog("close"); } 
        		}
       	}).dialog('open');
    })
    
    

        var clicko='';
       
        $(".mover").mousedown(function(){
             clicko='mover';
        });
   
        $(".copiar").mousedown(function(){
             clicko='copiar';
        });
   
   
       $( ".draggable" ).draggable({
            handle: '.mover, .copiar',
            cursor: "move",
     
            start: function(event, ui) {
				startleft = ui.position.left;
				startop = ui.position.top;
			},
            stop: function (event,ui)
            {
                    $(this).css('left',startleft);
                    $(this).css('top',startop);
            }

       });
      
      

      
      $( ".droppable" ).droppable({
            over: function(event, ui) { 
                $(this).css('border','3px dotted red')
            },
            out: function(event, ui) { 
                $(this).css('border','1px solid #C3D9FF')
            },
            
			 drop: function(event, ui) {
			 fecha=$(this).find('div').attr('id')
             id=$(ui.helper).attr('id')
            
             if(clicko=='mover')
             {
                   $li= $(ui.helper).detach();
                   $ul= $(this).find('ul')
                   
                   $li.appendTo($ul).css('top','0px').css('left','0px')
                   $(this).css('border','1px solid #C3D9FF')
                   
                   $.getJSON('<?=$this->baseUrl('intervenciones/move')?>', {date:fecha,id:id})
             }
             
             if(clicko=='copiar')
             {
                   $li= $(ui.helper).clone();
                   $ul= $(this).find('ul')
                   $li.appendTo($ul).css('top','0px').css('left','0px')
                   $(this).css('border','1px solid #C3D9FF')
     
                   $(this).find('a').attr('title','Recarga para mover , copiar')
                   $(this).find('.mover,.copiar').hide()
                   
                   $.get('<?=$this->baseUrl('intervenciones/copy')?>', {date:fecha,id:id}, function(data){
                       $li.attr('id','fila-'+data.id)
                       
                   })
                  
                  
                    
             }  
          }
		}); 
        
        
        
        
        $( ".mover" ).button({
			text: false,
			icons: {
				primary: "ui-icon-extlink"
			}
		});
		$( ".copiar" ).button({
			text: false,
			icons: {
				primary: "ui-icon-newwin"
			}
		});
		$( ".borrar_inter" ).button({
			text: false,
			icons: {
				primary: "ui-icon-trash"
			}
		})
        $( ".editar_inter" ).button({
			text: false,
			icons: {
				primary: "ui-icon-pencil"
			}
		})
        
        $(".butonset").buttonset();

        
        
        
        
});



/*
function copiar(id)
{
	$('#dialog').empty();	
	if(id!=0){
		$.post('/index.php', {ajax:'Intervenciones', action: 'form_edit2', id: id},function(data){
			$('#dialog').html('Copiar a :');
		});
        
		$('#dialog').dialog({buttons: { 
				"Guardar": function() { $("form#ajax_form").submit() } ,
				"Cancelar": function() { $(this).dialog("close"); } 
			}
		})
		$('#dialog').dialog('open');
		$('.fecha').datepicker();
	}
}
*/






</script>





<?=$busqueda?>

<div class="ui-widget" id="caja_magica"> 
	<div style="margin-top: 20px; padding: 0 .7em;width:150px;float:right"> 
	<p><span class="ui-icon ui-icon-circle-close" style="float: left; margin-right: .3em;"></span><?php echo $this->translate('Anulado');?></p> 
    <p><span class="ui-icon ui-icon-transferthick-e-w" style="float: left; margin-right: .3em;"></span><?php echo $this->translate('Reprogramado');?></p> 
	</div> 
	<div style="margin-top: 20px; padding: 0 .7em;width:150px;float:right"> 
	<p><span class="ui-icon ui-icon-clock" style="float: left; margin-right: .3em;"></span><?php echo $this->translate('En curso');?></p> 
    <p><span class="ui-icon ui-icon-circle-check" style="float: left; margin-right: .3em;"></span><?php echo $this->translate('Realizado');?></p> 
    </div> 
</div> 

<br />

<blockquote>*<?php echo $this->translate('Pulsa sobre el digito del dia para añadir una intervención');?></blockquote>

<div style="clear:both"></div>

<?foreach($this->calendario as $v)
{
    print $this->calendar($v['fecha'],$v['data']);
}?>



<div style="display: none;">
<textarea cols="60" rows="5" id="textarea"></textarea>
</div>
